package com.ngc.mall.controller;

import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.core.enumeration.MessageStatus;
import com.ngc.mall.domain.MessageContentDomain;
import com.ngc.mall.domain.MessageRecordDomain;
import com.ngc.mall.param.MessageContentParam;
import com.ngc.mall.service.IMessageContentService;
import com.ngc.mall.service.IMessageRecordService;
import com.ngc.mall.vo.MessageContentVO;
import com.ngc.mall.vo.MessageRecordVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 站内信控制类
 * @author: chenxiaolin
 * @date 2023/11/8 15:18
**/

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageContentService iMessageContentService;

    @Autowired
    private IMessageRecordService iMessageRecordService;


    @PostMapping("/send_all")
    @ApiOperation("发送给所有人")
    public ResponseResult<Long> sendToAllUser(@RequestBody MessageContentDomain domain){
        return ResponseResult.success(iMessageContentService.createMessageContent(domain));
    }

    @PostMapping("/send_single")
    @ApiOperation("发送给指定某个用户")
    @Transactional
    public ResponseResult<Long> sendToUser(@RequestBody MessageContentParam param){
        Long uid= LocalUser.getLocalUser().getId();
        MessageContentDomain domain= MessageContentDomain.builder().noticeType(param.getNoticeType()).content(param.getContent()).title(param.getTitle()).sendId(uid).build();
        Long msgId=iMessageContentService.createMessageContent(domain);
        MessageRecordDomain messageRecordDomain= MessageRecordDomain.builder().messageId(msgId).status(MessageStatus.UNREAD.getValue()).receiveId(param.getReceiveId()).build();
        iMessageRecordService.createMessageRecord(messageRecordDomain);
        return ResponseResult.success(msgId);
    }

    @PutMapping("/read/{cId}")
    @ApiOperation("用户已读消息")
    public ResponseResult<Boolean> updateStatusToReadById(@PathVariable Long cId){
         return ResponseResult.auto(iMessageRecordService.updateStatusToReadById(cId));
    }

    @PostMapping("/receive/{msgId}")
    @ApiOperation("创建用户接收消息(消息公告）")
    public ResponseResult<Boolean> createMessageRecord(@PathVariable Long msgId){
        Long uid=LocalUser.getLocalUser().getId();
        MessageRecordDomain messageRecordDomain=MessageRecordDomain.builder().receiveId(uid).status(MessageStatus.READ.getValue()).messageId(msgId).build();
        return ResponseResult.auto(iMessageRecordService.createMessageRecord(messageRecordDomain));
    }

    @PutMapping("/delete/{cId}")
    @ApiOperation("用户删除消息")
    public ResponseResult<Boolean> deleteById(@PathVariable Long cId){
        return ResponseResult.auto(iMessageRecordService.deleteById(cId));
    }

    @GetMapping("/send/all")
    @ApiOperation("获取所有发送消息记录")
    public ResponseResult<List<MessageContentVO>> queryAllMessageContent(){
        return ResponseResult.success(iMessageContentService.findAll());
    }

    @GetMapping("/receive/notice/myself")
    @ApiOperation("获取当前登录用户接收到的消息记录")
    public ResponseResult<List<MessageRecordVO>> queryAllNoticeByMySelf(){
        return ResponseResult.success(iMessageRecordService.getNoticeByMySelf());
    }

    @GetMapping("/receive/single-chat/myself")
    @ApiOperation("获取当前登录用户接收到的消息记录")
    public ResponseResult<List<MessageRecordVO>> queryAllSingleChatByMySelf(){
        return ResponseResult.success(iMessageRecordService.getSingleChatByMySelf());
    }
}


